using UnityEngine;

public class LevelItem : MonoBehaviour
{
	public int indexOfLevel;

	public UISprite spriteBlack;

	public UISprite spriteUpPart;

	public UISprite[] spriteStars;

	public UITexture textureCompletePart;

	public UITexture textureBg;

	public UILabel labelLevelValue;

	private void Start()
	{
		OnEnable();
	}

	private void OnEnable()
	{
		textureBg.mainTexture = Instance<UIChooseLevel>.instance.textureBgs[getIndex()];
		labelLevelValue.text = (indexOfLevel + 1).ToString();
		if (indexOfLevel == 0)
		{
			changeToAvailable();
			if (getStarCount() == 3)
			{
				changeToComplete();
			}
		}
		else if (getStarCount() == 3)
		{
			changeToComplete();
		}
		else if (Instance<UIChooseLevel>.instance.levelItems[indexOfLevel - 1].isCompleteQuest_1st())
		{
			if (indexOfLevel == 10)
			{
				if (Instance<UIChooseLevel>.instance.getTotalStarCount() >= 21)
				{
					changeToAvailable();
				}
				else
				{
					changeToLocked();
				}
			}
			else if (indexOfLevel == 20)
			{
				if (Instance<UIChooseLevel>.instance.getTotalStarCount() >= 45)
				{
					changeToAvailable();
				}
				else
				{
					changeToLocked();
				}
			}
			else if (indexOfLevel == 30)
			{
				if (Instance<UIChooseLevel>.instance.getTotalStarCount() >= 72)
				{
					changeToAvailable();
				}
				else
				{
					changeToLocked();
				}
			}
			else
			{
				changeToAvailable();
			}
		}
		else
		{
			changeToLocked();
		}
	}

	private void OnClick()
	{
		if (!spriteBlack.gameObject.active)
		{
			Instance<UIChooseLevel>.instance.onClickItem(indexOfLevel);
			Instance<AudioController>.instance.play(AudioType.Sound_tag);
		}
	}

	public void changeToComplete()
	{
		spriteBlack.gameObject.SetActiveRecursively(state: false);
		spriteUpPart.gameObject.SetActiveRecursively(state: true);
		for (int i = 0; i < 3; i++)
		{
			spriteStars[i].spriteName = "sprite_achievement_star";
		}
		textureCompletePart.gameObject.SetActiveRecursively(state: true);
		textureBg.mainTexture = Instance<UIChooseLevel>.instance.textureCompleteBg;
	}

	public void changeToLocked()
	{
		spriteBlack.gameObject.SetActiveRecursively(state: true);
		spriteUpPart.gameObject.SetActiveRecursively(state: false);
		textureCompletePart.gameObject.SetActiveRecursively(state: false);
		textureBg.mainTexture = Instance<UIChooseLevel>.instance.textureBgs[getIndex()];
	}

	public void changeToAvailable()
	{
		spriteBlack.gameObject.SetActiveRecursively(state: false);
		spriteUpPart.gameObject.SetActiveRecursively(state: true);
		if (isCompleteQuest_1st())
		{
			spriteStars[0].spriteName = "sprite_achievement_star";
			if (isCompleteQuest_2nd())
			{
				spriteStars[1].spriteName = "sprite_achievement_star";
				if (isCompleteQuest_3rd())
				{
					spriteStars[2].spriteName = "sprite_achievement_star";
				}
				else
				{
					spriteStars[2].spriteName = "sprite_achievement_star_gray";
				}
			}
			else
			{
				spriteStars[2].spriteName = "sprite_achievement_star_gray";
				if (isCompleteQuest_3rd())
				{
					spriteStars[1].spriteName = "sprite_achievement_star";
				}
				else
				{
					spriteStars[1].spriteName = "sprite_achievement_star_gray";
				}
			}
		}
		else
		{
			spriteStars[0].spriteName = "sprite_achievement_star_gray";
			spriteStars[1].spriteName = "sprite_achievement_star_gray";
			spriteStars[2].spriteName = "sprite_achievement_star_gray";
		}
		textureCompletePart.gameObject.SetActiveRecursively(state: false);
		textureBg.mainTexture = Instance<UIChooseLevel>.instance.textureBgs[getIndex()];
	}

	public int getStarCount()
	{
		if (!isCompleteQuest_1st())
		{
			return 0;
		}
		int num = 1;
		if (isCompleteQuest_2nd())
		{
			num++;
		}
		if (isCompleteQuest_3rd())
		{
			num++;
		}
		return num;
	}

	public bool isCompleteQuest_1st()
	{
		return GameData.LevelStars[indexOfLevel, 0];
	}

	public bool isCompleteQuest_2nd()
	{
		return GameData.LevelStars[indexOfLevel, 1];
	}

	public bool isCompleteQuest_3rd()
	{
		return GameData.LevelStars[indexOfLevel, 2];
	}

	public int getIndex()
	{
		if (MathHelper.isArrayHaveNum(GameHelper.getRateATeam(), LoadData.levelTeams[indexOfLevel]))
		{
			return 0;
		}
		if (MathHelper.isArrayHaveNum(GameHelper.getRateBTeam(), LoadData.levelTeams[indexOfLevel]))
		{
			return 1;
		}
		if (MathHelper.isArrayHaveNum(GameHelper.getRateCTeam(), LoadData.levelTeams[indexOfLevel]))
		{
			return 2;
		}
		if (MathHelper.isArrayHaveNum(GameHelper.getRateDTeam(), LoadData.levelTeams[indexOfLevel]))
		{
			return 3;
		}
		return 0;
	}
}
